package com.zros.modules.business.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zros.common.exception.JeecgBootException;
import com.zros.modules.business.dto.CreateClueDto;
import com.zros.modules.business.dto.QueryClueDto;
import com.zros.modules.business.dto.UpdateClueDto;
import com.zros.modules.business.entity.Audition;
import com.zros.modules.business.entity.Channel;
import com.zros.modules.business.entity.ChannelType;
import com.zros.modules.business.entity.Clue;
import com.zros.modules.business.entity.ClueCour;
import com.zros.modules.business.entity.Course;
import com.zros.modules.business.entity.Department;
import com.zros.modules.business.entity.EnrollCourse;
import com.zros.modules.business.entity.Teacher;
import com.zros.modules.business.entity.UpdateRecord;
import com.zros.modules.business.mapper.ClueMapper;
import com.zros.modules.business.service.IAuditionService;
import com.zros.modules.business.service.IChannelService;
import com.zros.modules.business.service.IChannelTypeService;
import com.zros.modules.business.service.IClueCourService;
import com.zros.modules.business.service.IClueService;
import com.zros.modules.business.service.ICourseService;
import com.zros.modules.business.service.IDepartmentService;
import com.zros.modules.business.service.IEnrollCourseService;
import com.zros.modules.business.service.ITeacherService;
import com.zros.modules.business.service.IUpdateRecordService;
import com.zros.modules.business.vo.AuditionVo;
import com.zros.modules.business.vo.ClueInfoVo;
import com.zros.modules.business.vo.EnrollCourseVo;
import com.zros.modules.business.vo.UpdateRecordVo;
import com.zros.modules.system.entity.Organization;
import com.zros.modules.system.entity.User;
import com.zros.modules.system.service.IOrganizationService;
import com.zros.modules.system.service.IUserService;

/**
 * <p>
 * 线索表 服务实现类
 * </p>
 *
 * @author pangyinqiang
 * @since 2019-04-16
 */
@Service
@Slf4j
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements IClueService {

	private static final String String = null;

	// 咨询信息
	@Autowired
	private IClueCourService clueCourService;

	// 试听信息
	@Autowired
	private IAuditionService auditionService;

	// 报名信息
	@Autowired
	private IEnrollCourseService enrollCourseService;

	// 跟进信息
	@Autowired
	private IUpdateRecordService updateRecordService;

	// 渠道信息
	@Autowired
	private IChannelService channelService;

	// 用户信息
	@Autowired
	private IUserService userService;

	// 校区信息
	@Autowired
	private IOrganizationService organizationService;

	// 渠道类型信息
	@Autowired
	private IChannelTypeService channelTypeService;

	// 科系信息
	@Autowired
	private IDepartmentService departmentService;

	// 课程信息
	@Autowired
	private ICourseService courseService;

	// 老师信息
	@Autowired
	private ITeacherService teacherService;

	@Autowired
	private ClueMapper clueMapper;

	@Override
	@Transactional
	public boolean createClue(CreateClueDto createClueDto) {
		// 校验
		QueryWrapper<Clue> ew = new QueryWrapper<>();
        ew.eq("customer_phone", createClueDto.getCustomerPhone()).
    	eq("customer_name", createClueDto.getCustomerName());
        List<Clue> tempList = this.list(ew);
        if (!CollectionUtils.isEmpty(tempList)) {
            throw new JeecgBootException("线索已存在，请不要重复添加!");
        }
		// insertj基本信息
		Clue clueEntity = new Clue();
		BeanCopier.create(CreateClueDto.class, Clue.class, false).copy(createClueDto, clueEntity, null);

		log.info("开始创建线索对象");

		List<Long> channelIdList = createClueDto.getChannelIdList();
		if (!CollectionUtils.isEmpty(channelIdList)) {
			clueEntity.setChannelId(channelIdList.get(1));
		}

		// 如果是否到访为0 ，则诺到访时间为null
		if ("0".equals(clueEntity.getIsPromiseVisit())) {
			clueEntity.setPromiseVisitDate(null);
		}
		// 如果确定时间为0 ，则诺到访时间为null
		if ("0".equals(clueEntity.getIsSureVisit())) {
			clueEntity.setSureVisitDate(null);
		}
		// 如果是否到访为0,则到访时间为null，如果是否到访为1，则未到访原因为null
		if ("0".equals(clueEntity.getIsVisit())) {
			clueEntity.setVisitTime(null);
		}

		// 预设一些字段的状态
		// 线索创建者（暂时设置为1）
		// clueEntity.setCreator(1L);
		// 跟进次数（0次）
		clueEntity.setUpdateCount(0);
		// 是否有效（有效）
		clueEntity.setIsAvailable("1");
		// 状态码（默认）
		clueEntity.setClueStatusCode("2");
		// 是否置顶（否）
		clueEntity.setIsTop("0");

		boolean result = this.save(clueEntity);
		if (result) {
			Long clueId = clueEntity.getClueId();
			log.info("创建线索对象成功，返回id" + clueId);

			// 2.咨询的课程信息
			log.info("开始创建咨询的课程信息");
			List<Long> clueCourIdList = createClueDto.getClueCourIdList();
			if (CollectionUtils.isNotEmpty(clueCourIdList)) {
				List<ClueCour> clueCourList = new ArrayList<ClueCour>();
				for (Long courseId : clueCourIdList) {
					ClueCour clueCour = new ClueCour();
					clueCour.setClueId(clueId);
					clueCour.setCourseId(courseId);
					clueCourList.add(clueCour);
				}
				clueCourService.saveBatch(clueCourList);
				log.info("结束创建咨询的课程信息,共创建" + clueCourList.size() + "条咨询的课程信息");
			}

			// 4.试听信息
			// 试听了，才会去创建试听记录表
			if ("1".equals(createClueDto.getIsAudition())) {
				List<Audition> auditionList = createClueDto.getAuditionList();
				if (CollectionUtils.isNotEmpty(auditionList)) {
					log.info("开始创建试听信息");
					for (Audition audition : auditionList) {
						audition.setClueId(clueId);
					}
					auditionService.saveBatch(auditionList);
					log.info("结束创建咨询的课程信息,共创建" + auditionList.size() + "条试听信息");
				} else {
					log.info("试听信息为空");
				}
			} else {
				log.info("没有试听，不需要创建试听记录");
			}

			// 5.报名信息
			if ("1".equals(createClueDto.getIsEnroll())) {
				List<EnrollCourse> enrollCourseList = createClueDto.getEnrollCourseList();
				if (CollectionUtils.isNotEmpty(enrollCourseList)) {
					log.info("开始创建线索的报名信息");
					for (EnrollCourse enrollCourse : enrollCourseList) {
						enrollCourse.setClueId(clueId);
					}
					enrollCourseService.saveBatch(enrollCourseList);
					log.info("结束创建线索的报名信息,共创建" + enrollCourseList.size() + "条报名信息");
				} else {
					log.info("报名信息为空");
				}
			} else {
				log.info("没有报名，不需要创建报名记录");
			}

		}
		log.info("结束创建线索");
		// 6.跟进信息
		return result;
	}

	@Override
	public ClueInfoVo info(Long clueId) {

		ClueInfoVo clueInfoVo = new ClueInfoVo();

		// 1.根据线索id查找到线索本身
		Clue clue = new Clue();
		clue = this.getById(clueId);
		if (null == clue) {
			throw new JeecgBootException("当前线索不存在");
		}
		
		BeanCopier.create(Clue.class, ClueInfoVo.class, false).copy(clue, clueInfoVo, null);

		// 校区id
		clueInfoVo.setClueId(String.valueOf(clue.getClueId()));
		clueInfoVo.setOrgId(String.valueOf(clue.getOrgId()));
		clueInfoVo.setOwnerId(String.valueOf(clue.getOwnerId()));

		// 1.1根据线索中的渠道id去查找渠道的类型id
		Long channelId = clue.getChannelId();
		Channel channel = new Channel();
		channel = channelService.getById(channelId);
		Long channelTypeId = channel.getChannelTypeId();
		List<String> channelIdList = new ArrayList<String>();
		channelIdList.add(String.valueOf(channelTypeId));
		channelIdList.add(String.valueOf(channelId));
		clueInfoVo.setChannelIdList(channelIdList);

		// 2.根据线索id查找咨询信息
		List<ClueCour> clueCourList = new ArrayList<ClueCour>();
		ClueCour clueCour = new ClueCour();
		clueCour.setClueId(clueId);
		QueryWrapper<ClueCour> queryWrapper = new QueryWrapper<ClueCour>(clueCour);
		clueCourList = clueCourService.list(queryWrapper);
		if (CollectionUtils.isNotEmpty(clueCourList)) {
			List<String> clueCourIdList = new ArrayList<String>();
			for (ClueCour cCour : clueCourList) {
				clueCourIdList.add(String.valueOf(cCour.getCourseId()));
			}
			clueInfoVo.setClueCourIdList(clueCourIdList);
		}

		// 3.根据线索id查找试听信息
		if ("1".equals(clue.getIsAudition())) {
			List<Audition> auditionList = new ArrayList<Audition>();
			Audition audition = new Audition();
			audition.setClueId(clueId);
			QueryWrapper<Audition> auditionQueryWrapper = new QueryWrapper<Audition>(audition);
			auditionList = auditionService.list(auditionQueryWrapper);
			List<AuditionVo> auditionVoList = new ArrayList<AuditionVo>();
			for (Audition aud : auditionList) {
				AuditionVo auditionVo = new AuditionVo();
				BeanCopier.create(Audition.class, AuditionVo.class, false).copy(aud, auditionVo, null);
				auditionVo.setTeacherId(String.valueOf(aud.getTeacherId()));
				auditionVo.setCourseId(String.valueOf(aud.getCourseId()));
				auditionVoList.add(auditionVo);
			}
			clueInfoVo.setAuditionList(auditionVoList);
		}

		// 4.根据线索id查找报名信息
		if ("1".equals(clue.getIsEnroll())) {
			List<EnrollCourse> enrollCourseList = new ArrayList<EnrollCourse>();
			EnrollCourse enrollCourse = new EnrollCourse();
			enrollCourse.setClueId(clueId);
			QueryWrapper<EnrollCourse> enrollCourseQueryWrapper = new QueryWrapper<EnrollCourse>(enrollCourse);
			enrollCourseList = enrollCourseService.list(enrollCourseQueryWrapper);
			List<EnrollCourseVo> enrollCourseVoList = new ArrayList<EnrollCourseVo>();
			for (EnrollCourse eCourse : enrollCourseList) {
				EnrollCourseVo enrollCourseVo = new EnrollCourseVo();
				BeanCopier.create(EnrollCourse.class, EnrollCourseVo.class, false).copy(eCourse, enrollCourseVo, null);
				enrollCourseVo.setEnrollCourseId(String.valueOf(eCourse.getEnrollCourseId()));
				enrollCourseVo.setClueId(String.valueOf(clueId));
				enrollCourseVo.setCourseId(String.valueOf(eCourse.getCourseId()));
				enrollCourseVoList.add(enrollCourseVo);
			}
			clueInfoVo.setEnrollCourseList(enrollCourseVoList);
		}

		// 5.根据线索id查找根进记录
		List<UpdateRecord> updateRecordList = new ArrayList<UpdateRecord>();
		UpdateRecord updateRecord = new UpdateRecord();
		updateRecord.setClueId(clueId);
		QueryWrapper<UpdateRecord> updateRecordQueryWrapper = new QueryWrapper<UpdateRecord>(updateRecord);
		updateRecordQueryWrapper.orderByDesc("create_time");
		updateRecordList = updateRecordService.list(updateRecordQueryWrapper);
		List<UpdateRecordVo> updateRecordVoList = new ArrayList<UpdateRecordVo>();
		for (UpdateRecord uRecord : updateRecordList) {
			UpdateRecordVo updateRecordVo = new UpdateRecordVo();
			BeanCopier.create(UpdateRecord.class, UpdateRecordVo.class, false).copy(uRecord, updateRecordVo, null);
			Long userId = uRecord.getCreator();
			User user = userService.getById(userId);
			if (null != user) {
				updateRecordVo.setCreatorName(user.getUserName());
			}
			updateRecordVo.setClueId(String.valueOf(clueId));
			updateRecordVo.setUpdateRecordId(String.valueOf(uRecord.getUpdateRecordId()));
			updateRecordVoList.add(updateRecordVo);
		}
		clueInfoVo.setUpdateRecordList(updateRecordVoList);
		// 组装成vo对象
		return clueInfoVo;
	}

	@Override
	@Transactional
	public boolean updateClue(UpdateClueDto updateClueDto) {
		// 校验

		// 1.判断当前线索是否存在
		Long clueId = updateClueDto.getClueId();
		Clue clue = new Clue();
		clue = this.getById(clueId);
		if (null == clue) {
			throw new JeecgBootException("当前线索不存在");
		}
		
		QueryWrapper<Clue> ew = new QueryWrapper<>();
        ew.ne("clue_id", updateClueDto.getClueId()).and(e -> e.eq("customer_phone", updateClueDto.getCustomerPhone())
        		).and(e -> e.eq("customer_name", updateClueDto.getCustomerName()));
                //.eq("role_name", role.getRoleKey())
        List<Clue> tempList = this.list(ew);
        if (!CollectionUtils.isEmpty(tempList)) {
            throw new JeecgBootException("线索已存在，请不要重复添加");
        }

		// 转换基本信息
		Clue clueEntity = new Clue();
		BeanCopier.create(UpdateClueDto.class, Clue.class, false).copy(updateClueDto, clueEntity, null);

		log.info("开始修改线索对象");

		log.info("解析vue下拉框组件，获取线索的渠道信息");
		List<Long> channelIdList = updateClueDto.getChannelIdList();
		if (!CollectionUtils.isEmpty(channelIdList)) {
			clueEntity.setChannelId(channelIdList.get(1));
		}

		// 如果是否诺到访为0 ，则诺到访时间为null，是否确定时间为否，确定时间为null
		if ("0".equals(clueEntity.getIsPromiseVisit())) {
			// 诺到访时间
			clueEntity.setPromiseVisitDate(null);
			// 是否确定时间
			clueEntity.setIsSureVisit("0");
			// 确定时间
			clueEntity.setSureVisitDate(null);
		}
		// 如果诺到，再判断是否确定时间了
		else {
			// 如果确定时间为0 ，则诺到访时间为null
			if ("0".equals(clueEntity.getIsSureVisit())) {
				clueEntity.setSureVisitDate(null);
			}
		}

		// 如果没有到访，那么就要把到访时间清空，也要把试听信息都清空
		if ("0".equals(clueEntity.getIsVisit())) {
			log.info("没有到访");
			// 清空到访时间
			clueEntity.setVisitTime(null);

			// 如果没有到访，那么是否试听也改为否
			clueEntity.setIsAudition("0");

			// 清空试听信息
			HashMap<String, Object> idMap = new HashMap<String, Object>();
			idMap.put("clue_id", clueId);
			log.info("开始删除该线索对应的试听信息");
			auditionService.removeByMap(idMap);
			log.info("结束删除该线索对应的试听信息");
		}
		// 如果到访了，就将未到访原因清空，再判断有没有试听
		else {
			// 未到访原因清空
			clueEntity.setNoVisitReason("");

			// 试听了，才会去创建试听记录表
			if ("1".equals(updateClueDto.getIsAudition())) {
				List<Audition> auditionList = updateClueDto.getAuditionList();
				if (CollectionUtils.isNotEmpty(auditionList)) {
					// 先删再加
					HashMap<String, Object> idMap = new HashMap<String, Object>();
					idMap.put("clue_id", clueId);
					log.info("开始删除该线索对应的试听信息");
					auditionService.removeByMap(idMap);
					log.info("结束删除该线索对应的试听信息");

					log.info("开始创建试听信息");
					for (Audition audition : auditionList) {
						audition.setClueId(clueId);
						audition.setTryCourseId(null);
					}
					auditionService.saveBatch(auditionList);
					log.info("结束修改咨询的课程信息,共创建" + auditionList.size() + "条试听信息");
				} else {
					log.info("试听了，试听信息为空");
				}
			} else {
				log.info("没有试听");

				// 如果是误操作，也要把原先的删掉
				// 先删再加
				HashMap<String, Object> idMap = new HashMap<String, Object>();
				idMap.put("clue_id", clueId);
				log.info("开始删除该线索对应的试听信息");
				auditionService.removeByMap(idMap);
				log.info("结束删除该线索对应的试听信息");
			}
		}

		// 2.咨询的课程信息
		log.info("开始修改咨询的课程信息");
		List<Long> clueCourIdList = updateClueDto.getClueCourIdList();
		if (CollectionUtils.isNotEmpty(clueCourIdList)) {
			// 先删
			HashMap<String, Object> idMap = new HashMap<String, Object>();
			idMap.put("clue_id", clueId);
			log.info("开始删除该线索对应的课程信息");
			clueCourService.removeByMap(idMap);
			log.info("结束删除该线索对应的课程信息");

			// 再加
			log.info("开始增加该线索对应的课程信息");
			List<ClueCour> clueCourList = new ArrayList<ClueCour>();
			for (Long courseId : clueCourIdList) {
				ClueCour clueCour = new ClueCour();
				clueCour.setClueId(clueId);
				clueCour.setCourseId(courseId);
				clueCour.setRelId(null);
				clueCourList.add(clueCour);
			}
			clueCourService.saveBatch(clueCourList);
			log.info("结束增加该线索对应的课程信息");
			log.info("结束修改咨询的课程信息,共修改" + clueCourList.size() + "条咨询的课程信息");
		}

		// 5.报名信息，如果报名了，就要把未报名原因和未报名原因补充清空,再添加报名列表
		if ("1".equals(updateClueDto.getIsEnroll())) {
			// 未报名原因
			clueEntity.setNoEnrollReason("");
			clueEntity.setNoEnrollReasonRemark("");

			// 报名列表
			List<EnrollCourse> enrollCourseList = updateClueDto.getEnrollCourseList();
			if (CollectionUtils.isNotEmpty(enrollCourseList)) {
				// 先删再加
				HashMap<String, Object> idMap = new HashMap<String, Object>();
				idMap.put("clue_id", clueId);
				log.info("开始删除该线索对应的报名信息");
				enrollCourseService.removeByMap(idMap);
				log.info("结束删除该线索对应的报名信息");

				log.info("开始创建线索的报名信息");
				for (EnrollCourse enrollCourse : enrollCourseList) {
					enrollCourse.setClueId(clueId);
					enrollCourse.setEnrollCourseId(null);
				}
				enrollCourseService.saveBatch(enrollCourseList);
				log.info("结束创建线索的报名信息,共创建" + enrollCourseList.size() + "条报名信息");
			} else {
				log.info("报名了，但是报名信息为空");
			}
		}
		// 如果没有报名，那么要把报名课程列表都清空
		else {
			log.info("没有报名，既不需要新增报名列表，同时也要把原有的报名信息都清空");
			// 没有报名，既不需要新增报名列表，同时也要把原有的报名信息都清空
			HashMap<String, Object> idMap = new HashMap<String, Object>();
			idMap.put("clue_id", clueId);
			log.info("开始删除该线索对应的报名信息");
			enrollCourseService.removeByMap(idMap);
			log.info("结束删除该线索对应的报名信息");
		}

		// 6.跟进信息
		// 设置跟进信息，更新线索的跟进次数，跟进人为当前用户，会由mybatis插件注入
		if (StringUtils.isNotEmpty(updateClueDto.getUpdateInfo())) {
			// 记录一下修改次数,下面如果updateInfo不为空，则需要将修改次数自增
			int updateCount = clue.getUpdateCount();
			log.info("开始添加跟进记录");
			UpdateRecord updateRecord = new UpdateRecord();
			updateRecord.setClueId(clueId);
			updateRecord.setUpdateInfo(updateClueDto.getUpdateInfo());
			if (updateRecordService.save(updateRecord)) {
				clueEntity.setUpdateCount(updateCount + 1);
			} else {
				throw new JeecgBootException("添加跟进信息失败");
			}
			log.info("结束添加跟进记录");
		}
		boolean result = this.updateById(clueEntity);
		if (!result) {
			throw new JeecgBootException("编辑线索失败");
		}
		log.info("结束修改线索");
		return result;
	}

	@Override
	public Page<ClueInfoVo> selectClueList(Page<ClueInfoVo> page, QueryClueDto queryClueDto) {
		Page<ClueInfoVo> pageUserInfo = new Page<ClueInfoVo>();
		List<ClueInfoVo> clueInfoVoList = clueMapper.selectClueList(queryClueDto,(page.getCurrent()-1)*page.getSize(),page.getSize());

		Map<String, String> dataMap = new HashMap<String, String>();
		Map<String, String> useDataMap = new HashMap<String, String>();

		// 查询所有用户id和名称
		useDataMap.putAll(userService.list().stream()
				.collect(Collectors.toMap(User::getStringUserId, User::getUserName, (key1, key2) -> key2)));
		// 查询所有校区
		dataMap.putAll(organizationService.list().stream().collect(
				Collectors.toMap(Organization::getIdStr, Organization::getOrganizationName, (key1, key2) -> key2)));
		// 查询所有渠道类型
		dataMap.putAll(channelTypeService.list().stream().collect(Collectors.toMap(ChannelType::getStringChannelTypeId,
				ChannelType::getChannelTypeName, (key1, key2) -> key2)));
		// 查询所有渠道
		dataMap.putAll(channelService.list().stream()
				.collect(Collectors.toMap(Channel::getStringChannelId, Channel::getChannelName, (key1, key2) -> key2)));
		// 查询所有科系
		dataMap.putAll(departmentService.list().stream().collect(Collectors.toMap(Department::getStringDepartmentId,
				Department::getDepartmentName, (key1, key2) -> key2)));
		// 查询所有课程
		dataMap.putAll(courseService.list().stream()
				.collect(Collectors.toMap(Course::getStringCourseId, Course::getCourseName, (key1, key2) -> key2)));
		// 查询所有老师
		dataMap.putAll(teacherService.list().stream()
				.collect(Collectors.toMap(Teacher::getStringTeacherId, Teacher::getTeacherName, (key1, key2) -> key2)));

		for (ClueInfoVo clueInfoVo : clueInfoVoList) {
			clueInfoVo.setOrgName(dataMap.get(clueInfoVo.getOrgId()));
			clueInfoVo.setCreatorName(useDataMap.get(clueInfoVo.getCreator()));
			if (null != clueInfoVo.getOperator()) {
				clueInfoVo.setOperatorName(useDataMap.get(clueInfoVo.getOperator().toString()));
			}

			clueInfoVo.setOwnerName(useDataMap.get(clueInfoVo.getOwnerId()));

			// 线索是否有效 格式==>是否有效（状态码）
			clueInfoVo.setClueStatusText(clueInfoVo.getIsAvailableText() + clueInfoVo.getClueStatusCodeText());

			// 渠道 格式==>渠道（渠道类型）
			clueInfoVo.setChannelInfo(
					clueInfoVo.getChannelName() + "(" + dataMap.get(clueInfoVo.getChannelTypeId()) + ")");

			// 跟进记录
			for (UpdateRecordVo updateRecordVo : clueInfoVo.getUpdateRecordList()) {
				updateRecordVo.setCreatorName(useDataMap.get(updateRecordVo.getCreator().toString()));
			}

			// 咨询课程
			for (ClueCour clueCour : clueInfoVo.getClueCourList()) {
				clueCour.setCourseName(dataMap.get(clueCour.getCourseId().toString()));
			}

			// 试听课程
			for (AuditionVo auditionVo : clueInfoVo.getAuditionList()) {
				auditionVo.setCourseName(dataMap.get(auditionVo.getCourseId()));
				auditionVo.setTeacherName(dataMap.get(auditionVo.getTeacherId()));
			}

			// 报名课程
			for (EnrollCourseVo enrollCourseVo : clueInfoVo.getEnrollCourseList()) {
				enrollCourseVo.setCourseName(dataMap.get(enrollCourseVo.getCourseId()));
			}
			
			//跟进记录排序
			ListSort(clueInfoVo.getUpdateRecordList());
		}
		
		Long count = clueMapper.selectCountClueList(queryClueDto);
		pageUserInfo.setTotal(count);
		pageUserInfo.setRecords(clueInfoVoList);
		return pageUserInfo;
	}

	@Override
	public Page<ClueInfoVo> selectNotifyClueList(Page<ClueInfoVo> page, QueryClueDto queryClueDto) {
		Page<ClueInfoVo> pageUserInfo = clueMapper.selectNotifyClueList(page, queryClueDto);
		return pageUserInfo;
	}

	@Override
	public boolean removeClue(Long id) {

		// 判断当前线索是否存在
		Long clueId = id;//updateClueDto.getClueId();
		Clue clue = new Clue();
		clue = this.getById(clueId);
		if (null == clue) {
			throw new JeecgBootException("当前线索不存在");
		}
		
		// 删除基本信息
		this.removeById(id);
		
		HashMap<String, Object> idMap = new HashMap<String, Object>();
		idMap.put("clue_id", clueId);
		
		// 清空试听信息
		log.info("开始删除该线索对应的试听信息");
		auditionService.removeByMap(idMap);
		log.info("结束删除该线索对应的试听信息");

		// 先删咨询的课程信息
		log.info("开始删除该线索对应的课程信息");
		clueCourService.removeByMap(idMap);
		log.info("结束删除该线索对应的课程信息");

		// 报名信息
		log.info("开始删除该线索对应的报名信息");
		enrollCourseService.removeByMap(idMap);
		log.info("结束删除该线索对应的报名信息");

		// 跟进信息
		log.info("开始删除该线索对应的跟进信息");
		updateRecordService.removeByMap(idMap);
		log.info("结束删除该线索对应的跟进信息");

		return false;
	}
	
	private  void ListSort(List<UpdateRecordVo> list) 
	{
        Collections.sort(list, new Comparator<UpdateRecordVo>() {
           @Override
            public int compare(UpdateRecordVo u1, UpdateRecordVo u2) {
        	  //DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
              try {
                   if (u2.getCreateTime().isAfter(u1.getCreateTime())) {
                       return 1;
                    } else if (u2.getCreateTime().isBefore(u1.getCreateTime())) {
                        return -1;
                    } else {
                        return 0;
                    }
                } catch (Exception e) {
                  e.printStackTrace();
                }
                return 0;
            }
       });
    }
}
